const Login = require('../models/login_model')
const jsonResult = require('../utils/JSON')
const Cookie = require('../utils/cookie')
const Common = require('../utils/common')
const Menu = require('../models/menu_model')
const Hospital = require('../models/hospital_model')

exports.loginCheck = async (ctx, next) => {
    try {
        let { body } = ctx.request.body;
        let res = await Login.loginCheck({ admin_name: body.id, admin_password: body.pass });
        let role = res.admin_role == 1 ? ctx.body = jsonResult.resultSuccessJson(undefined, undefined, { msg: res.msg, token: role }) : ctx.body = jsonResult.resultErrorJson(undefined, undefined);
        Cookie.setCookie(ctx, 'token',res.h_id);
        Cookie.setCookie(ctx, 'admin_password',res.admin_password);
        Cookie.setCookie(ctx, 'admin_id',res.admin_id);
    } catch (error) {
        console.log(error)
        ctx.body = jsonResult.resultErrorJson(undefined, undefined);
    }
}

exports.loginRoleInfo = async (ctx) => {
    try {
        let admin_id = Cookie.checkoutCoolie(ctx,'admin_id');
        console.log(admin_id,'admin_id');
        let admin_password = Cookie.checkoutCoolie(ctx,'admin_password');
        console.log(admin_password,'admin_password');
        let res = await Login.loginRoleInfo({ admin_name:admin_id, admin_password:admin_password });
        let hospitalList = await Hospital.hospitalSelect({ pageIndex: 1, pageSize: 1, hi_id: res.h_id });
        let menuList = await Menu.menuGet(hospitalList.list[0].hi_menu.split(','));
        let menuInfo = Common.menuTree(hospitalList.list[0].hi_id, hospitalList.list[0].hi_name, menuList);
        ctx.body = jsonResult.resultSuccessJson(undefined, undefined, { msg: '用户信息', menuInfo: menuInfo, userInfo: res });
    }catch (error) {
    console.log(error);
    ctx.body = jsonResult.resultErrorJson(undefined, undefined);
}
}

